<?php
/**  取已完成的订单去s.12301上面取
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-10-23
 * Time: 上午9:12
 */

use \PFT\Db;
include_once '/var/www/html/new/d/module/common/Db.class.php';
$dbConf = include_once '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息
Db::Conf($dbConf['localhost']);
$con_0 = Db::connect('0');

Db::Conf($dbConf['remote_1']);
$con_1 = Db::connect('1');

Db::Close('1');
Db::Close('0');
require_once("/var/www/html/new/d/common/func.inc.php");//checkData
/*
 * @param aid
 * @param aid
 * @param btime
 * @param etime
 * @param tid
 * @param lid
 * */


//处理传入的参数
if($_GET['landid'] && !$_GET['ticket']){
    $sql="select id from uu_jq_ticket where landid=? ";
    $stmt = $con_0->prepare($sql);
    $stmt->execute(array($_GET['landid']));
    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
        $tid .= $row['id'].',';
    }
    $tid = rtrim($tid,',');
    if($tid=='') goto bottom;
}else{

    $tid = $_GET['ticket']+0;

}


$query = array();
$where .= "ddate>=? and ddate<=?";
$query[] = checkData(substr($_GET['begintime'],0,10))? substr($_GET['begintime'],0,10): date('Y-m-d',$_SERVER['REQUEST_TIME']-86400);
$_GET['endtime'] =  checkData(substr($_GET['endtime'],0,10))  ? substr($_GET['endtime'],0,10)  : date('Y-m-d',$_SERVER['REQUEST_TIME']-86400);
$query[] = $_GET['endtime'];

if($tid){
    $where .= " and tid in ($tid)";
}
if($_GET['aid']+0 && $_GET['fid']+0){
    $where .= " and aid=? and mid=?";
    $query[] = $_GET['aid']+0;
    $query[] = $_GET['fid']+0;

}elseif($_GET['aid']+0 && ($_GET['fid']+0==0)){
    $where .= " and aid=? and mid!=?";
    $query[] = $_GET['aid']+0;
    $query[] = $_GET['aid']+0;
}elseif($_GET['fid']+0){
    $where .= " and mid=?";
    $query[] = $_GET['fid']+0;
}
//根据统计类型的不同 先取到他们的名称 景区取景区名,会员取会员名
//景区还要多做一步 构造一个lid_tid的关联数组

$chengben_arr = array();
if($by=='lid' || $by=='tid'){
    $sql = "select tid from  pft_order_statistics where $where group by tid";

    $stmt = $con_1->prepare($sql);
    $stmt->execute($query);
    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
        $tids .= $row['tid'].',';
    }

    $tids = rtrim($tids,',');
    $sql = "select l.id as lid,l.title as ltitle,t.id as tid,t.title as ttitle from uu_land l left join uu_jq_ticket t on l.id=t.landid where t.id in ($tids)";
    $stmt = $con_0->prepare($sql);
    $stmt->execute();
    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
        $tid_lid[$row['tid']] = $row;
    }

    $by_ = 'tid';
}else{//by=aid||mid

    $sql = "select $by from  pft_order_statistics where $where group by $by";

    $stmt = $con_1->prepare($sql);
    $stmt->execute($query);
    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
        $amid .= $row[$by].',';
    }
    $amid = rtrim($amid,',');
    $sql = "select id,dname from pft_member where id in ($amid)";
    $stmt = $con_0->prepare($sql);
    $stmt->execute();
    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
        $dname[$row['id']]=$row['dname'];
    }
    $by_ = $by;
}

$xml = new stdClass();
$xml->Rec= array();
if($_GET['endtime'] == date('Y-m-d',$_SERVER['REQUEST_TIME'])){
    $s = new SoapClient(null,array(
            "location" => "http://localhost/open/openService/pft_insideMX.php",
            "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
    );
    $xml = simplexml_load_string($s->Order_Globle_Search( "",$_GET['fid'], $option['lid'], $option['tid'], "", "", "", "",
        "", "",$_GET['endtime'].' 00:00:00', $_GET['endtime'].' 23:59:59', "", "", "", "", "", "", "", "",0, 500, 0, 0, "", 0, "", 0, "",
        "",$_GET['aid'], 0, 0, 0, ""));
    // var_dump($xml);
}


$sql="select sum(lirun) as lirun,$by_,sum(torder) as `order`,sum(tnum) as tnum,sum(money) as money,sum(pmode0) as pmode0,
    sum(pmode1) as pmode1,sum(pmode2) as pmode2,sum(pmode3) as pmode3,sum(pmode4) as pmode4,sum(pmode5) as pmode5,sum(pmode8) as pmode8,sum(pmode7) as pmode7
    from pft_order_statistics where $where  group by $by_ ";
// echo $sql;

// if($_GET['endtime'] == '2015-11-12'){
//     $xml = simplexml_load_string($s->Order_Globle_Search( "",$_GET['fid'], "", "", "", "", "", "",
//         "", "",$_GET['endtime'].' 00:00:00', $_GET['endtime'].' 23:59:59', "", "", "", "", "", "", "", "",0, 500, 0, 0, "", 0, "", 0, "",
//         "",$_GET['aid'], 0, 0, 0, ""));
// }


// $sql="select sum(lirun*tnum) as lirun,$by_,sum(torder) as `order`,sum(tnum) as tnum,sum(money) as money,sum(pmode0) as pmode0,
//     sum(pmode1) as pmode1,sum(pmode2) as pmode2,sum(pmode3) as pmode3,sum(pmode4) as pmode4,sum(pmode5) as pmode5,sum(pmode8) as pmode8,sum(pmode7) as pmode7
//     from pft_order_statistics where $where and 0   group by $by_ ";

$stmt = $con_1->prepare($sql);
$stmt->execute($query);
//$row = $stmt->fetchAll(\PDO::FETCH_ASSOC);
//echo $sql;var_dump($tid);
//exit;
if($by=='lid'){
    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
        $r[$tid_lid[$row['tid']]['lid']]['title'] = $tid_lid[$row['tid']]['ltitle'];
        $r[$tid_lid[$row['tid']]['lid']]['order'] += $row['order'];
        $r[$tid_lid[$row['tid']]['lid']]['tnum'] += $row['tnum'];
        $r[$tid_lid[$row['tid']]['lid']]['money'] += $row['money'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode0'] += $row['pmode0'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode1'] += $row['pmode1'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode2'] += $row['pmode2'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode3'] += $row['pmode3'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode4'] += $row['pmode4'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode5'] += $row['pmode5'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode7'] += $row['pmode7'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode8'] += $row['pmode8'];
        $r[$tid_lid[$row['tid']]['lid']]['pmode8'] += $row['pmode8'];
        $r[$tid_lid[$row['tid']]['lid']]['lirun'] += $row['lirun'];

    }
}elseif($by=='tid'){
    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
        $row['title'] = $tid_lid[$row['tid']]['ltitle']. $tid_lid[$row['tid']]['ttitle'];
        $r[$row['tid']] = $row;
    }
}else{//by=aid||mid
    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
        $row['title'] = $dname[$row[$by]];
        $r[$row[$by]] = $row;
    }
    $by_dname = 'UU'.$by;
    include '/var/www/html/new/d/getDistors.php';
    $memberID = ($_SESSION['dtype']==6 || $_SESSION['dtype']==7) ? $_SESSION['sid']:$_SESSION['memberID'];
    if($_SESSION['dtype']==7){
        $DistorsNames = getAll($GLOBALS['le'],$memberID);
    }else{
        $DistorsNames = getRetail($GLOBALS['le'],$memberID);
    }
    $DistorsNames = getRetail($GLOBALS['le'],$memberID);
}
//今天的数据
foreach($xml->Rec as $v){
    // var_dump($v);
    if ($by == 'lid') {
        $tag = (string)$v->UUlid;
        $title = (string)$v->UUltitle;
    } elseif ($by == 'tid') {
        $tag = (string)$v->UUtid;
        $title = (string)$v->UUltitle.(string)$v->UUttitle;
    } else {
        $tag = (string)$v->UUbuyid;
        $title = substr($DistorsNames[$tag],2);
    }
    $r[$tag]['title'] =  $title;
    $r[$tag]['order'] += 1;
    $r[$tag]['tnum'] += (int)$v->UUtnum;
    if ($_GET['aid']) {
        if((string)$v->UUaids!='0'){
            $UUaids = (string)$v->UUaids.','.(string)$v->UUmid;
            $UUaids_price = (string)$v->UUaids_price.','.(string)$v->UUtprice;
        }else{
            $UUaids_price = (string)$v->UUtprice;
            if($v->UUaid!==$v->UUmid){
                $UUaids = (string)$v->UUaid.','.(string)$v->UUmid;
            }else{
                $UUaids = (string)$v->UUaid;
            }
        }
        $UUaids = explode(',', $UUaids);
        $UUaids_price = explode(',', $UUaids_price);
        $count = count($UUaids);
        if ((string)$v->UUaids) {
            $key = array_search($_GET['aid'], $UUaids);
            $price = $UUaids_price[$key];
        } else{
            $price = (int)$v->UUtprice;
        }
        $r[$tag]['money'] += (int)$v->UUtnum*(int)$price;
        if ($count == 1) {
            $UUpmode = (int)$v->UUpaystatus==0? 'pmode4' : 'pmode3';
        } else {
            if ((string)$v->UUaids_money) {
                if ($v->UUaid == $_GET['aid']) {
                    $UUpmode = 'pmode'.(string)$v->UUpmode;
                } else {
                    $tmp_info = unserialize($v->UUaids_money);
                    $pmode = $tmp_info[$key][1] == 1 ? '0' : '2';
                    $UUpmode = 'pmode'. $pmode;
                }
            } else {
                $UUpmode = 'pmode'.(string)$v->UUpmode;
            }
        }
        $r[$tag][$UUpmode] += (int)$v->UUtnum*(int)$price;

        if ((string)$v->UUaids != 0) {
            $lirun = ($UUaids_price[$key] - (int)$v->UUaprice) * (int)$v->UUtnum;
        } else {
            $lirun = ((int)$v->UUtprice - (int)$v->UUaprice) * (int)$v->UUtnum;
        }

        $r[$tag]['lirun'] += $lirun;
    } elseif ($_GET['fid']) {
        $totalmoney = 0;
        if ((string)$v->UUaids_money) {
            $key = array_search($_GET['fid'], explode(',', $v->UUaids));
            $price_arr = explode(',', $v->UUaids_price);
            if ($key > 0) {
                $totalmoney= $price_arr[$key -1] * (int)$v->UUtnum;
            } else {
                $totalmoney= $v->UUtprice * (int)$v->UUtnum;
            }
            $tmp_info = unserialize($v->UUaids_money);
            $pmode = $tmp_info[$key][1] == 1 ? '0' : '2';
            $UUpmode = 'pmode'. $pmode;
            // $UUpmode = 'pmode' . (int)$v->UUpmode == 1 ? 0 : 2;
        } else {
            $totalmoney= (int)$v->UUtprice * (int)$v->UUtnum;
            $UUpmode = 'pmode' . (int)$v->UUpmode;
        }
        $r[$tag]['money'] += $totalmoney;
        // var_dump($UUpmode);
        // var_dump((int)$v->UUtotalmoney);
        $r[$tag][$UUpmode] += $totalmoney;
    }
    // for ($i = 0; $i <= 8; $i++) {
    //     if ($UUpmode != 'pmode'.$i) {
    //         $r[$tag]['pmode'.$i] = 0;
    //     }
    // }
}
if (count($r) > 0) {
    foreach ($r as $key => $val) {
        for ($i = 0; $i <= 8; $i++) {
            if (!isset($r[$key]['pmode'.$i])) {
                $r[$key]['pmode'.$i] = 0;
            }
        }
    }
}
goto bottom1;

bottom:
$r = array();
bottom1:
$con_1 = $con_0 = null;












